package chess;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class for Servlet: ExitRoom
 *
 */
 public class ExitRoom extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public ExitRoom() {
		super();
	}   	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String roomid = request.getParameter("rid");
		String seat = request.getParameter("seat");
		GameHall gameHall = (GameHall)(ContextHelper.getInstance().get("GameHall"));
		GameRoom room  = gameHall.getRoom(roomid);
		if(seat.equals("A")){
			room.setSeatA(null);
			room.setSeatAready(false);
			if(room.getSeatB()!=null){
				room.addMSG("B", new RoomUserMSG("","",""));
			}
		}else{
			room.setSeatB(null);
			room.setSeatBready(false);
			if(room.getSeatA()!=null){
				room.addMSG("A", new RoomUserMSG("","",""));
			}
		}
		if(room.getStatus().equals("active")){
			room.setStatus("inactive");
		}
	}   	  	    
}